package com.hlee.regex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;


public class StringSubstitution
{

	public static void main(String[] args)
	{
		Map<String, String> hMap = new HashMap<String, String>();
		hMap.put(null, "s");
		System.out.println(hMap);
		
		Map<String, String> tmap = new TreeMap<String, String>();
		tmap.put(null, "s");
		System.out.println(tmap);
		
		
//		Map<String, String> ht = new Hashtable<String, String>();
//		ht.put(null, "s");
//		System.out.println(ht);
		

		List<String> al = new ArrayList<String>();
		al.add(null);
		System.out.println(al);
		
		List<String> list2 = new Vector<String>();
		list2.add(null);
		System.out.println(list2);
		
		Set<String> set1 = new HashSet<String>();
		set1.add(null);
		System.out.println(set1);
		
		Set<String> set2 = new TreeSet<String>();
		set2.add(null);
		System.out.println(set2);
		
		
		
//		testRegex2();
		
//		test1();
		test2();
		
//		regexTest1();
		
//		regex1();
		//testRegex();		
		//testRegex1();		
		//test();
		
		
//		testZip(330000);
//		testZip(70700000);
//		testZip(70700123);
//		testZip(100010000);
		
		
		
	}

	private static void testRegex2() {
		String str1 = "\\\\<\\\\!\\\\-\\\\-\\\\s*v3ahahstart\\\\(' + contentId + '\\\\)\\\\s*\\\\-\\\\-\\\\>";
		boolean matched = str1.matches(str1);
		System.out.println("str1: " + str1);
		System.out.println("matched: " + matched);
		
	}
	
	private static void testZip(long zipCode) {
		Long zip9 = new Long(zipCode);
		System.out.println("zip: " + zip9);
		
		String zipString = StringUtils.leftPad(zip9.toString(), 9, '0');
		if (zipString.substring(5).equals("0000"))
			zipString = zipString.substring(0, 5);
		else
			zipString = zipString.substring(0, 5) + "-" + zipString.substring(5);
		
		System.out.println("zip string: " + zipString + "\n");
	}
	
	
	
	
	private static void testRegex1() {
		
		String message = "[RECTANGLE]\r\nX = 986\r\n[HIGHLIGHT]\r\nX = 917";
		System.out.println("original:\n" + message);
		
		message = message.replaceAll("]\r\n", "]\r\nVIEW = 1\r\n");
		System.out.println("altered:\n" + message);
		
		message = message.replaceAll("]\n", "]\r\nVIEW = 1\n");
		System.out.println("altered:\n" + message);

		
	}

	private static void testRegex() {
		String message = "Dear ${first_name} ${last_name}, Welcome to our Game Club. We will contact you via you email ${email}.";
		System.out.println("message: " + message);

		message = message.replaceAll("\\$\\{first_name\\}", "Mickey");
		message = message.replaceAll("\\$\\{last_name\\}", "Mouse");
		message = message.replaceAll("\\$\\{email\\}", "mail@mail.com");
		System.out.println("message: " + message + "\n");

		String docParams = "page1=\"${page1}\"\n" + 
				"annotationFile=\"${annotationFile}\"\n" +
				"thumbLabel1=\"${thumbLabel1}\"\n";
		System.out.println("docParams:\n" + docParams);
		
		docParams = docParams.replaceAll("\\$\\{page1\\}", "this is page1");
		docParams = docParams.replaceAll("\\$\\{annotationFile\\}", "this is annotation");
		docParams = docParams.replaceAll("\\$\\{thumbLabel1\\}", "this is thumbLabel1");
		System.out.println("docParams:\n" + docParams);
	}
	
	private static void test()
	{
		String regex = "'|\"";
		String source = "I'm \"Heonkoo\"";
		String result = source.replaceAll(regex, "`");
		
		System.out.println("source: " + source);
		System.out.println("result: " + result);
	}

	private static void regex1() {
		//sample string: {\field{\*\fldinst{\f3\fs22\cf1 BK:IMAGE_BOOKMARK1}}{\fldrslt{\f3\fs22\cf1 [BK:IMAGE_BOOKMARK1]}}}
		String source = "{\\field{\\*\\fldinst{\\f3\\fs22\\cf1 BK:IMAGE_BOOKMARK1}}{\\fldrslt{\\f3\\fs22\\cf1 [BK:IMAGE_BOOKMARK1]}}}";
		String result = source.replace("\\", "%");
		System.out.println("source: " + source);
		System.out.println("result: " + result);
		
	}
	
	// ['
	// \.
	// {\field{\*\fldinst{\f3\fs22\cf1 BK:IMAGE_BOOKMARK1}}{\fldrslt{\f3\fs22\cf1 [BK:IMAGE_BOOKMARK1]}}}
	// {\field{\*\fldinst{\f3\fs22 BK:IMAGE_BOOKMARK1}}{\fldrslt{\f3\fs22 [BK:IMAGE_BOOKMARK1]}}}
	
	private static void regexTest1() {
		
        String regex = "\\w{2}\\s\\d{2}"; // \w{2} = [a-zA-Z_0-9]{2}
        regex = "[A-Za-z]{2}\\s\\d{2}";
        regex = "[a-e]";
        regex = "\\{a\\}";
        regex = "\\{\\\\field\\{\\\\\\*\\\\fldinst\\{\\\\f\\d{1}\\\\fs\\d{2}\\\\cf\\d{1}\\sBK:\\w{1,50}}}\\{\\\\fldrslt\\{\\\\f\\d{1}\\\\fs\\d{2}\\\\cf\\d{1}\\s\\[BK:\\w{1,50}]}}}";
        regex = "\\{\\\\field\\{\\\\\\*\\\\fldinst\\{\\\\f\\d{1}\\\\fs\\d{2}(\\\\cf\\d{1})?\\sBK:\\w{1,50}}}\\{\\\\fldrslt\\{\\\\f\\d{1}\\\\fs\\d{2}(\\\\cf\\d{1})?\\s\\[BK:\\w{1,50}]}}}";
        
        String s1 = "=={\\field{\\*\\fldinst{\\f3\\fs22\\cf1 BK:IMAGE_BOOKMARK1}}{\\fldrslt{\\f3\\fs22\\cf1 [BK:IMAGE_BOOKMARK1]}}}==";
        String s2 = "=={\\field{\\*\\fldinst{\\f3\\fs22 BK:IMAGE_BOOKMARK1}}{\\fldrslt{\\f3\\fs22 [BK:IMAGE_BOOKMARK1]}}}==";
        String s3 = s1 + s2;
        
        printRegexTest(regex, s1);
//        printRegexTest(regex, s2);
//        printRegexTest(regex, s3);
	}
	
	private static void printRegexTest(String regex, String input) {
		Pattern pattern = Pattern.compile(regex);
		System.out.println("regex: " + regex);        
		Matcher matcher = pattern.matcher(input); 
		System.out.println("regex found in " + input + "? " + matcher.find());   
		int start = matcher.start();
		System.out.println("start index of match: " + start);
		String replacement = "{\\\\pict\\\\pngblip}";
		System.out.println("replace with " + replacement + ": " + input.replaceAll(regex, replacement));
		System.out.println("replace with " + replacement + ": " + matcher.replaceAll(replacement));
		System.out.println();
	}
	
	private static void test1() {
		String regex = "java";
		String input = "java programmer, java developer";
		Pattern p = Pattern.compile(regex);
		Matcher matcher = p.matcher(input);
		System.out.println("- regex: " + regex);
		System.out.println("- input string: " + input);
		
		while (matcher.find()) {
			System.out.println("match start: " + matcher.start());
			System.out.println("match end  : " + matcher.end());
			System.out.println("matcher group: " + matcher.group());
			System.out.println();
		}
	}
	
	private static void test2() {
		String regex = "\\A[A-Z]."; // \A means match only start of the text
		String input = "Working Day and Night";
		Pattern p = Pattern.compile(regex);
		Matcher matcher = p.matcher(input);
		System.out.println("- regex: " + regex);
		System.out.println("- input string: " + input);

//		if (matcher.lookingAt()) {
//			System.out.println("matched on the beginning of input text");
//			System.out.println("matcher group: " + matcher.group());
//			System.out.println("match start: " + matcher.start());
//			System.out.println("match end  : " + matcher.end());
//			System.out.println("=================================\n");
//		}
		
		int count = 0;
		while (matcher.find()) {
			count++;
			System.out.println("match start: " + matcher.start());
			System.out.println("match end  : " + matcher.end());
			System.out.println("matcher group: " + matcher.group());
			System.out.println();
		}
		
		if (count == 0)
			System.out.println("no match found.");
	}
}
